package com.jianyun.jyzs.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hannesdorfmann.mosby3.mvp.MvpActivity;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.adapter.AnnouncementListAdapter;
import com.jianyun.jyzs.base.MVPBaseActivity;
import com.jianyun.jyzs.bean.AnnouncementList;
import com.jianyun.jyzs.dao.AnnouncementDao;
import com.jianyun.jyzs.dao.ModelTools;
import com.jianyun.jyzs.presenter.AnnouncementPresenter;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.view.iview.IAnnouncementsView;
import com.jianyun.jyzs.widget.ErecyclerView;
import com.jrmf360.rylib.common.util.ToastUtil;
import com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.jianyun.jyzs.utils.RceLog;

/**
 * 通知公告界面
 * Created by yangshijun on 2018/5/29.
 */

public class AnnouncementActivity extends MvpActivity<IAnnouncementsView,
        AnnouncementPresenter> implements IAnnouncementsView, View.OnClickListener,
        SwipyRefreshLayout.OnRefreshListener {

    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.tv_checked)
    TextView tvChecked;
    @BindView(R.id.tv_update)
    TextView tvUpdate;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.iv_moreLinearLayout)
    LinearLayout ivMoreLinearLayout;
    @BindView(R.id.topCreat)
    LinearLayout topCreat;
    @BindView(R.id.iv_single)
    ImageView ivSingle;
    @BindView(R.id.iv_group)
    ImageView ivGroup;
    @BindView(R.id.topChatDetail)
    LinearLayout topChatDetail;
    @BindView(R.id.topId)
    RelativeLayout topId;
    @BindView(R.id.loadButton)
    Button loadButton;
    @BindView(R.id.loadRl)
    RelativeLayout loadRl;
    @BindView(R.id.emptyview)
    RelativeLayout emptyview;
    @BindView(R.id.recyclerView)
    ErecyclerView recyclerView;
    @BindView(R.id.swipy)
    SwipyRefreshLayout swipy;


    private AnnouncementDao dao;
    private int page = 1;
    private String enterpriseCode = "";
    private AnnouncementListAdapter adapter;
    private List<AnnouncementList.Announcement> announmentList;
    private String userId;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_announcement);
        UtilsStyle.setStatusBarMode(this,true);
        ButterKnife.bind(this);
        initData();
        initView();
    }


    private void initData() {
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        userId = LoginCache.getInstance().getUserInfo().getUserId();
        announmentList = new ArrayList<>();
        topBack.setVisibility(View.VISIBLE);
        topBack.setOnClickListener(this);
        topText.setText("通知公告");
        recyclerView.setEmptyView(emptyview);
        swipy.setOnRefreshListener(this);

    }

    private void initView() {
        dialog = new ProgressDialog(this);
        dialog.setMessage("loading...");
        dao = new AnnouncementDao(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AnnouncementListAdapter(dao, this, null);
        recyclerView.setAdapter(adapter);
        //从服务器取到本地保存
        HashMap<String, String> map = new HashMap<>();
        map.put("enterpriseCode", enterpriseCode);
        map.put("page", page + "");
        //getPresenter().getAnnouncementList(dao,map);
        //从服务器取到本地保存
        getPresenter().getAnnouncement(dao, enterpriseCode,userId, page, 20);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ModelTools.clearNotify(this, userId, enterpriseCode, "通知公告");
        setAnnouncementView();
    }

    private void setAnnouncementView() {
        announmentList = dao.getAnnounmentList(enterpriseCode);
        for (AnnouncementList.Announcement aa : announmentList
                ) {
            RceLog.d("lml", "getMsgCount=" + aa.getMsgCount() + "getAgreeCount=" + aa.getAgreeCount());
        }
        adapter.setList(announmentList);

    }

    @NonNull
    @Override
    public AnnouncementPresenter createPresenter() {
        return new AnnouncementPresenter();
    }

    @Override
    public void onSuccess(int size) {
        setAnnouncementView();
        swipy.setRefreshing(false);
    }

    @Override
    public void onFaile(String msg) {
        ToastUtil.showNoWaitToast(this, msg);
        swipy.setRefreshing(false);
    }

    @Override
    public void showLoading() {
        dialog.show();
    }

    @Override
    public void hintLoading() {
        dialog.dismiss();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.topBack:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh(SwipyRefreshLayoutDirection direction) {
        page += 1;
        getPresenter().getAnnouncement(dao, enterpriseCode,userId, page, 20);
    }
}
